package com.zlkj.pro.modular.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.consts.ProjectConstants;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.core.util.FunctionUtil;
import com.zlkj.pro.modular.api.model.dto.ResourcesDTO;
import com.zlkj.pro.modular.api.model.vo.ResourcesVO;
import com.zlkj.pro.modular.api.service.ResourcesService;
import com.zlkj.pro.modular.common.entity.ResourcesEntity;
import com.zlkj.pro.modular.common.mapper.ResourcesMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 孙铭泽
 * @date 2022/8/30
 * @time 14:55
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, ResourcesEntity> implements ResourcesService {

    @Override
    public Map<String, ResourcesVO> findResourceByIdentifies(ResourcesDTO resourcesDTO) {
        String[] identifies = resourcesDTO.getIdentifies().split(",");
        LambdaQueryWrapper<ResourcesEntity> resourceEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        resourceEntityLambdaQueryWrapper.in(ResourcesEntity::getIdentifies, Arrays.asList(identifies));
        resourceEntityLambdaQueryWrapper.eq(ResourcesEntity::getStatusFlag, ProjectConstants.StatusFlag.ENABLE);
        List<ResourcesEntity> resourcesEntityList = super.list(resourceEntityLambdaQueryWrapper);
        List<ResourcesVO> resourcesVOList = ConvertUtil.turn(resourcesEntityList, ResourcesVO.class);
        return FunctionUtil.toMap(resourcesVOList, ResourcesVO::getIdentifies);
    }

    @Override
    public ResourcesVO findResourceByIdentifiesOnly(ResourcesDTO resourcesDTO) {
        String identifies = resourcesDTO.getIdentifies();
        LambdaQueryWrapper<ResourcesEntity> resourceEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        resourceEntityLambdaQueryWrapper.eq(ResourcesEntity::getIdentifies, identifies);
        resourceEntityLambdaQueryWrapper.eq(ResourcesEntity::getStatusFlag, ProjectConstants.StatusFlag.ENABLE);
        resourceEntityLambdaQueryWrapper.last("LIMIT 1");
        ResourcesEntity resourcesEntity = super.getOne(resourceEntityLambdaQueryWrapper);
        return ConvertUtil.turn(resourcesEntity, ResourcesVO.class);
    }
}
